﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin.Accounts.User
{
    public partial class ModifySelf : PageBase
    {
        private string UserID
        {
            get
            {
                return Common.UserInfo.User.UserID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserID == "")
            {
                Response.Write("传入不正确");
                Response.End();
                return;
            }

            if (!Page.IsPostBack)
            {
                if (UserID != "")
                {
                    ShowInfo();
                }
            }
        }

        private void ShowInfo()
        {
            Vincent.Permission.Model.account_userlist model = Vincent.Permission.Functions.Account.GetSingleUser(UserID); //bll.GetModel(UserID);
            this.txtUserName.Text = model.UserName;
            this.txtUserPwd.Text = model.UserPwd;
            this.txtTrueName.Text = model.TrueName;
            this.ddlSex.SelectedValue = model.Sex;
            this.txtPhone.Text = model.Phone;
            this.txtEmail.Text = model.Email;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                strErr += "用户名不能为空！\\n";
            }
            if (this.txtUserPwd.Text.Trim().Length == 0)
            {
                strErr += "密码不能为空！\\n";
            }
            if (this.txtTrueName.Text.Trim().Length == 0)
            {
                strErr += "真实姓名不能为空！\\n";
            }

            if (PageValidate.IsPhone(this.txtPhone.Text.Trim()))
            {
                strErr += "手机格式不正确！\\n";
            }
            if (!PageValidate.IsEmail(this.txtEmail.Text.Trim()))
            {
                strErr += "Email格式不正确！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string UserName = this.txtUserName.Text;
            string UserPwd = this.txtUserPwd.Text;
            string TrueName = this.txtTrueName.Text;
            string Sex = this.ddlSex.SelectedValue;
            string Phone = this.txtPhone.Text;
            string Email = this.txtEmail.Text;
            int UserType = 1;

            Vincent.Permission.Model.account_userlist model = Vincent.Permission.Functions.Account.GetSingleUser(UserID); //new Model.account_userlist();
            model.UserID = UserID;
            model.UserName = UserName;
            model.UserPwd = UserPwd;
            model.TrueName = TrueName;
            model.Sex = Sex;
            model.Phone = Phone;
            model.Email = Email;
            model.UserType = UserType;

            Vincent.Permission.Functions.Account.UpdateUser(model);
            MessageBox.ShowAndRedirect(this, "保存成功！", "Show.aspx?UserID=" + UserID);

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("Show.aspx?UserID=" + UserID);
        }
    }
}